/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.pacman.manager.bluetooth;

import sg.edu.nus.iss.pacman.manager.*;
import sg.edu.nus.iss.pacman.net.bluetooth.client.*;
import sg.edu.nus.iss.pacman.midlet.PacManScreen;

/**
 * A manager class for bluetooth client.
 * @author fredy
 */
public class BluetoothClientManager implements BluetoothClientListener, GameChallengeSender
{
    private BluetoothDevice[] bluetoothDevices;
    private BluetoothClient client;
    private PacManScreen pacMan;
    private int bluetoothDeviceIndex;

    /**
     * Creates a new insance of BluetoothClientManager.
     * @param screen the screen
     */
    public BluetoothClientManager(PacManScreen screen)
    {
        client = new BluetoothClient(this);
        this.pacMan = screen;
    }

    /**
     * Searches the remote devices.
     */
    public void searchRemoteDevices()
    {
        client.searchRemoteDevices();
    }

    /**
     * Cancels the search remote devices.
     */
    public void cancelSearchRemoteDevices()
    {
        client.interrupt();
    }

    public void found(BluetoothDevice[] bluetoothDevices)
    {
        this.bluetoothDevices = bluetoothDevices;
        String[] devices = new String[bluetoothDevices.length];
        for (int i = 0; i < devices.length; i++)
        {
            devices[i] = (bluetoothDevices[i]).getFriendlyName();
        }
        pacMan.listBluetoothDevices(devices);
    }

    public void sent()
    {
        pacMan.displayInfo("Bluetooth message has been successfully sent");
    }

    public void sendChallenge()
    {
        client.sendMessage(bluetoothDevices[bluetoothDeviceIndex], 
            GameChallengeSender.CHALLENGE_MESSAGE);
    }

    /**
     * Gets the bluetooth device index no.
     * @return the bluetooth device index no
     */
    public int getBluetoothDeviceIndex()
    {
        return bluetoothDeviceIndex;
    }

    /**
     * Sets the bluetooth device index no.
     * @param bluetoothDeviceIndex the bluetooth device index no
     */
    public void setBluetoothDeviceIndex(int bluetoothDeviceIndex)
    {
        this.bluetoothDeviceIndex = bluetoothDeviceIndex;
    }
}
